<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Balance extends Model
{
    public $table = 'user_balance';
    public static function addRecord($input)
    {
        if ($input['origin'] !=3){
            $input['img'] = 'http://img.slf0319.com/logo.png';
        }
        $input['created_at'] = $input['updated_at'] = date('Y-m-d H:i:s');
        $flag = self::query()->insert($input);
        return $flag;
    }


    /**添加购物明细
     * @param $order_sn
     * @return bool
     */
    public static function addShoppingRecord($order_sn)
    {
        $order = new Order();
        $order_detail = $order-> api_order_list(['orders.order_sn'=>$order_sn]);
        if(empty($order_detail)){
           return true;
        }
        $order_detail = $order_detail[0];
        self::addRecord([
            'title' => '购物',
            'img'   => !empty($order_detail['goods_list']) ? $order_detail['goods_list'][0]['image']:'',
            'type'  => 1,
            'origin'=> 3,
            'uid'   => $order_detail['user_id'],
            'amount' => $order_detail['money_paid'],
        ]);
    }

    /**明细列表
     * @param $postData
     * @return array
     */
    public static function balance_list($postData)
    {
        $query = self::query()->where('uid','=',$postData['uid']);
        if(!empty($postData['start_time']) && !empty($postData['finish_time'])){
            $begin_time = strtotime($postData['start_time']);
            $end_time = strtotime($postData['finish_time'])+24*3600;
//            $query -> whereBetween('created_at',[$postData['start_time'],$postData['finish_time']])
            $query -> whereRaw('unix_timestamp(created_at) >= '.$begin_time)
                   -> whereRaw('unix_timestamp(created_at) < '.$end_time);
            $list['use'] =(clone $query) -> where('type','=','1') -> sum('amount');
            $list['income'] = (clone $query) -> where('type','=','2') -> sum('amount');
            $list['date'] = $postData['start_time'].'至'.$postData['finish_time'];
            $list['list'] = $query -> get();
            return [$list];
        }else if(empty($postData['start_time']) && empty($postData['finish_time'])){
            return self::balance($postData);
        }
    }


    public static function balance($input)
    {
        $tmp = self::query()->where('uid',$input['uid'])->orderBy('id','desc')->get()->toArray();
        $tmp2 = [];
        foreach ($tmp as $key=>$val) {
            $val['key'] = date('Y-m',strtotime($val['created_at']));
            $tmp2[ $val['key']][] = $val;
        }
        $data = [];
        foreach ($tmp2 as $key=>&$val) {
            $income = 0;
            $use    = 0;
            foreach ($val as &$v){
                unset($v['key']);
                if ($v['type']==1){
                    $use    += $v['amount'];
                }else{
                    $income += $v['amount'];
                }
            }
            $data[$key]['use']      = $use;
            $data[$key]['income']   = $income;
            $data[$key]['date']     = $key ;
            $data[$key]['list']     = $val;

        }
        return array_values($data);
    }


}
